// @ts-ignore
import Stack from './Stack-Array.ts'


/**
 * 递归和栈的方式解决十进制转二进制
 * @param num
 * @param stack
 */
const decToBin = (num: number, stack: Stack<number> = new Stack<number>()) => {
  let str = ''
  const remainNum = num % 2
  stack.push(remainNum)
  num = Math.floor(num / 2)

  if (num > 0) {
    str = decToBin(num, stack)
  }

  while (!stack.isEmpty()) {
    str += stack.pop()
  }
  return str

}

/**
 * 栈和循环的方式解决十进制转二进制
 * @param num
 */
const decToBinByLoop = (num: number) => {
  const stack = new Stack<number>()
  while (num > 0) {
    const remainNum = num % 2
    stack.push(remainNum)
    num = Math.floor(num / 2)
  }

  let str = ''
  while (!stack.isEmpty()) {
    str += stack.pop()
  }

  return str
}

/**
 * 有效括号问题
 * @param str
 */
const isValid = (str: string) => {
  const toRight = ['(', '[', '{']
  const map = {
    '(': ')',
    '[': ']',
    '{': '}'
  }

  type keyType = keyof typeof map

  const stack = new Stack<string>()

  for (const key of str) {
    if (toRight.includes(key)) {
      stack.push(key)
      continue
    }
    const item = stack.pop() as keyType
    if (map[item] !== key) return false
  }

  return true


}





